# -*-coding:utf-8 -*-
# @Time: 2023/2/20 0020 下午 14:33
# @Author: Cwoner
# @Organization: CTIM
# @Software: PyCharm


import requests
from lxml import etree
import hashlib
from setting import SAVEPATH
import os
import re
import time


from tools.serverAPI import upload_file

class DaQingWanBao():
    def __init__(self,ccdate,id=''):#20230101
        self.url = f'https://epaper.dqdaily.com/wb/pc/layout/{ccdate[:6]}/{ccdate[6:]}/node_A01.html'
        self.ccdate = ccdate
        self.id = id
        self.name = '大庆晚报'
        print('初始化：',self.name, self.url)
        self.mid = hashlib.md5((self.name+'_baozi').encode()).hexdigest()
        if not os.path.isdir(SAVEPATH+self.mid):
            os.mkdir(SAVEPATH+self.mid)


    def index(self):
        headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
        }
        response = requests.get(self.url, headers=headers, verify=False)
        if response.status_code == 200:
            text = response.content.decode('utf-8')
            html = etree.HTML(text)
            pdfs = html.xpath('//div[@class="Chunkiconlist"]/p')
            for pdfItem in pdfs:
                i = 0
                title = pdfItem.xpath('./a/text()')[0]
                pdf_url = pdfItem.xpath('./a/@href')[1]
                pdf_url = pdf_url.replace('../../../', '')
                if not title:
                    title = f'未知标题{i}'
                title = re.sub('\s', '', title)
                print(self.name, title)
                tid = hashlib.md5((self.name + title + self.ccdate).encode()).hexdigest()
                file = tid + '.pdf'
                if pdf_url:
                    pdf_url = 'https://epaper.dqdaily.com/wb/pc/' + pdf_url.replace('../../../', '')
                    print(pdf_url)
                    self.__download(file, pdf_url, tid)



    def getImage(self,pid):
        headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Referer": "https://wbnews.dqdaily.com/dqwb/Pages/Zzdq/Historty.aspx?pid=1141",
            "Sec-Fetch-Dest": "iframe",
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-Site": "same-origin",
            "Sec-Fetch-User": "?1",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64wa; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
            "sec-ch-ua": "\"Chromium\";v=\"110\", \"Not A(Brand\";v=\"24\", \"Google Chrome\";v=\"110\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\""
        }
        url = "https://wbnews.dqdaily.com/dqwb/Pages/Zzdq/Periodicals.aspx"
        params = {
            "id": pid
        }
        response = requests.get(url, headers=headers,  params=params)
        if response.status_code == 200:
            text = response.text
            html = etree.HTML(text)
            srcs = html.xpath('//img[@class="editions-img"]/@data-src')
            return srcs





    def __download(self,file,url,tid):
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
        }
        response = requests.get(url, headers=headers, verify=False)
        if response.status_code == 200:
            path = SAVEPATH + self.mid + '/' + self.ccdate + '/'
            if not os.path.isdir(path):
                os.mkdir(path)
            with open(path + file,'wb',) as f:
                f.write(response.content)
            data = {
                'entity_id': self.mid,
                'title': url,
                'tid': tid,
                'file_name': file,
                'origin_url': url,
                'ndate': self.ccdate[:4] + '-' + self.ccdate[4:6] + '-' + self.ccdate[6:]
            }
            upload_file(data, response.content)
            return True
        else:
            return False


    def dispatcher(self):
        pid = self.index()
        if pid:
            srcs = self.getImage(pid)
            for img in srcs:
                title = img
                print(title)
                tid = hashlib.md5(img.encode()).hexdigest()
                file = tid + '.jpg'
                self.__download(file, title,tid)




    def run(self):
        self.dispatcher()





if __name__ == '__main__':
    ccdate = '20230905'
    hr = DaQingWanBao(ccdate)
    hr.run()



